package com.apache.funds.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.funds.entity.SvmVoucher;
import com.apache.funds.manager.SvmVoucherManager;

/**
 * 本类主要涉及业务逻辑<br>
 * @author Administrator
 *
 */
public class SvmVoucherManagerImpl implements SvmVoucherManager {
	protected IDao svmVoucherDao;
	protected final String entityName = "com.apache.funds.entity.SvmVoucher";

	public void setSvmVoucherDao(IDao svmVoucherDao) {
		this.svmVoucherDao = svmVoucherDao;
	}

	public String saveInfo(ParamsVo<SvmVoucher> vo) throws BusinessException {
		SvmVoucher svmVoucher = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="svmVoucherDetail_"+infoId;
		//svmVoucherDetail.setVoucherDetailNum(infoId);
		MethodParam param = new MethodParam("SvmVoucher", cacheKey, "", entityName);
		param.setVaule(svmVoucher);
		if (svmVoucherDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<SvmVoucher> vo) throws BusinessException {
		SvmVoucher svmVoucher = vo.getObj();
		if (Validator.isNotNull(svmVoucher.getVoucherId())) {
			String cacheKey = "";
			//String cacheKey="svmVoucher_"+svmVoucher.getVoucherId();
			MethodParam param = new MethodParam("SvmVoucher", cacheKey, "", entityName);
			param.setVaule(svmVoucher);
			return svmVoucherDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<SvmVoucher> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="svmVoucher_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		SvmVoucher info = (SvmVoucher) svmVoucherDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("SvmVoucher");
			return svmVoucherDao.edit(param);
		} else {
			param.setParams("voucherId", infoId);
			param.setDelete(true);
			return svmVoucherDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<SvmVoucher> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="svmVoucher_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return svmVoucherDao.selectById(param);
	}

	public Object execute(ParamsVo<SvmVoucher> vo) {
		return null;
	}

	public Page getPageInfo(ParamsVo<SvmVoucher> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = svmVoucherDao.pageSelect(param);
		return page;
	}

	public List<SvmVoucher> getList(ParamsVo<SvmVoucher> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return svmVoucherDao.select(param);
	}

	public long countInfo(ParamsVo<SvmVoucher> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return svmVoucherDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<SvmVoucher> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(svmVoucherDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		SvmVoucher svmVoucher = vo.getObj();
		if (Validator.isNotNull(svmVoucher.getTradeRecordId())) {
			sb.append(" and tradeRecordId = :tradeRecordId");
			param.setParams("tradeRecordId", svmVoucher.getTradeRecordId());
		}
		if (Validator.isNotNull(svmVoucher.getRemark())) {
			sb.append(" and remark = :remark");
			param.setParams("remark", svmVoucher.getRemark());
		}

		if (Validator.isNotNull(String.valueOf(vo.getParams("userId")))) {
			param.setParams("userId", String.valueOf(vo.getParams("userId")));
		}

		param.setSqlStr(sb.toString());
		return param;
	}

}
